package com.legrand.www.module.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;

import javax.servlet.http.HttpServletRequest;

public class FeignZuulFilter extends ZuulFilter {
    /*需要认证的请求地址*/
    private static final String FILTER_URL = "/seca";
    @Value("${zuul.ignored-headers}")//${eureka.client.serviceUrl.defaultZone}
    private String aa;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    /**
     * @author xiah
     * description: 判断接口是否需要登录认证
     * param:
     * return:
     * date: 2018/5/2
     */
    @Override
    public boolean shouldFilter() {

        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        return request.getRequestURL().toString().toLowerCase().contains(FILTER_URL);
    }

    /**
     * @author xiah
     * description: 如果需要登录认证就调用run方法
     * param:
     * return:
     * date: 2018/5/2
     */
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String name = request.getHeader("xiah");
        if ("fxf217".equals(name)) {
            System.out.println("通过feign调用的请求");
            return null;
        }
        System.out.println("不是通过feign调用的请求");
        return null;
        //return new RequestContextUtil(ctx, 403, "无权限访问!");

    }
}

